//
//  RONetworkMngTool.swift
//  NeuFood
//
//  Created by yangjingxin on 2017/10/12.
//  Copyright © 2017年 yangjingxin. All rights reserved.
//

import UIKit

let single = RONetworkMngTool()

class RONetworkMngTool: NSObject {
    
    class func sharedNetworkMngTool() -> RONetworkMngTool{
        return single
    }
    
    //escaping逃逸闭包，这个闭包在函数结束后在调用
    func RONetwork_Logon(_ parameters: NSDictionary, view: UIView, block: @escaping(_ flag: NSString)-> Void){
        //在开始网络加载前任务指示器开始转
        let RO_Logon: String = "http://60.205.189.39/userLogin.do"
        
        MBProgressHUD.showAdded(to: view, animated: true)
        
        let afManager = AFHTTPRequestOperationManager()
        afManager.get(RO_Logon, parameters: parameters, success: { (oeration: AFHTTPRequestOperation?, responseObject:Any?) in
            
            //print(responseObject)
            MBProgressHUD.hide(for: view, animated: true)
            //将返回的字典转换为模型对象
            let logonReturnModel = ROLogonReturnModel.logonReturnModelWithDict(responseObject as! NSDictionary)
            //当成功登陆后保存用户名和密码
            if logonReturnModel.userid != "0"{
                
                block("1")
            }
            else{
                MBProgressHUD.showDelayHUDToView(view, message: logonReturnModel.msg as String)
                block("0")
            }
            
        }) { (operation: AFHTTPRequestOperation?, error:Error?)in
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
            block("0")
        }
    }
    
}
